﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace JQueryControlToolkit
{
    public class AjaxQuery : JSObject
    {
        public string Accepts { get; set; }
        public bool? Async { get; set; }
        public DomOperation BeforeSend { get; set; }
        public bool? Cache { get; set; }
        public DomOperation Complete { get; set; }
        public string Content { get; set; }
        public string ContentType { get; set; }
        public DomResult Context { get; set; }
        public JSObject Converters { get; set; }
        public bool? CrossDomain { get; set; }
        public JSObject Data { get; set; }
        public JSFunction DataFilter { get; set; }
        public string DataType { get; set; }
        public DomOperation Error { get; set; }
        public bool? Global { get; set; }
        public Dictionary<string, string> Headers { get; set; }
        public bool? IfModified { get; set; }
        public bool? IsLocal { get; set; }
        public string Jsonp { get; set; }
        public string JsonpCallback { get; set; }
        public string MimeType { get; set; }
        public string Password { get; set; }
        public bool? ProcessData { get; set; }
        public string ScriptCharset { get; set; }
        public Dictionary<int, DomOperation> StatusCode { get; set; }
        public DomOperation Success { get; set; }
        public int? Timeout { get; set; }
        public bool? Traditional { get; set; }
        public string Type { get; set; }
        public string Url { get; set; }
        public string UserName { get; set; }
        public DomOperation Xhr { get; set; }
        public JSObject XhrFields { get; set; }


        #region JSObject Members

        public string ToScript()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter writer = new StringWriter(sb);
            ToScript(writer);
            return sb.ToString();
        }

        public void ToScript(System.IO.TextWriter toScript)
        {
            toScript.Write("{");
            toScript.Write("url:");
            DomOperation.ToScript(toScript, Url);

            if (!string.IsNullOrEmpty(Accepts))
            {
                toScript.Write(",");
                toScript.Write("accepts:");
                DomOperation.ToScript(toScript, Accepts);
            }
            if (Async.HasValue)
            {
                toScript.Write(",");
                toScript.Write("async:");
                DomOperation.ToScript(toScript, Async.Value);
            } if (BeforeSend != null)
            {
                toScript.Write(",");
                toScript.Write("beforeSend:");
                DomOperation.ToScript(toScript, BeforeSend);
            }
            if (Cache.HasValue)
            {
                toScript.Write(",");
                toScript.Write("cache:");
                DomOperation.ToScript(toScript, Cache.Value);
            }
            if (Complete != null)
            {
                toScript.Write(",");
                toScript.Write("complete:");
                DomOperation.ToScript(toScript, Complete);
            }
            if (!string.IsNullOrEmpty(Content))
            {
                toScript.Write(",");
                toScript.Write("content:");
                DomOperation.ToScript(toScript, Content);
            }
            if (!string.IsNullOrEmpty(ContentType))
            {
                toScript.Write(",");
                toScript.Write("contentType:");
                DomOperation.ToScript(toScript, ContentType);
            }
            if (Context != null)
            {
                toScript.Write(",");
                toScript.Write("context:'");
                DomOperation.ToScript(toScript, Context);
            }
            if (Converters != null)
            {
                toScript.Write(",");
                toScript.Write("converters:");
                DomOperation.ToScript(toScript, Converters);
            }
            if (CrossDomain.HasValue)
            {
                toScript.Write(",");
                toScript.Write("crossDomain:");
                DomOperation.ToScript(toScript, CrossDomain.Value);
            }
            if (!string.IsNullOrEmpty(ContentType))
            {
                toScript.Write(",");
                toScript.Write("contentType:");
                DomOperation.ToScript(toScript, ContentType);
            }
            if (Data != null)
            {
                toScript.Write(",");
                toScript.Write("data:");
                DomOperation.ToScript(toScript, Data);
            }
            if (DataFilter != null)
            {
                toScript.Write(",");
                toScript.Write("dataFilter:");
                DomOperation.ToScript(toScript, DataFilter);
            }
            if (!string.IsNullOrEmpty(DataType))
            {
                toScript.Write(",");
                toScript.Write("dataType:");
                DomOperation.ToScript(toScript, DataType);
            }
            if (Error != null)
            {
                toScript.Write(",");
                toScript.Write("error:");
                DomOperation.ToScript(toScript, Error);
            }
            if (Global.HasValue)
            {
                toScript.Write(",");
                toScript.Write("global:");
                DomOperation.ToScript(toScript, Global.Value);
            }
            if (Headers != null && Headers.Count > 0)
            {
                toScript.Write(",");
                toScript.Write("headers:");
                DomOperation.ToScript(toScript, Headers);
            }
            if (IfModified.HasValue)
            {
                toScript.Write(",");
                toScript.Write("ifModified:");
                DomOperation.ToScript(toScript, IfModified.Value);
            }
            if (IsLocal.HasValue)
            {
                toScript.Write(",");
                toScript.Write("isLocal:");
                DomOperation.ToScript(toScript, IsLocal);

            }
            if (!string.IsNullOrEmpty(Jsonp))
            {
                toScript.Write(",");
                toScript.Write("jsonp:");
                DomOperation.ToScript(toScript, Jsonp);
            }
            if (!string.IsNullOrEmpty(JsonpCallback))
            {
                toScript.Write(",");
                toScript.Write("jsonpCallback:");
                DomOperation.ToScript(toScript, JsonpCallback);

            }
            if (!string.IsNullOrEmpty(MimeType))
            {
                toScript.Write(",");
                toScript.Write("mimeType:");
                DomOperation.ToScript(toScript, MimeType);

            }

            if (!string.IsNullOrEmpty(Password))
            {
                toScript.Write(",");
                toScript.Write("password:");
                DomOperation.ToScript(toScript, Password);

            }
            if (ProcessData.HasValue)
            {
                toScript.Write(",");
                toScript.Write("processData:");
                DomOperation.ToScript(toScript, ProcessData.Value);
            }
            if (!string.IsNullOrEmpty(ScriptCharset))
            {
                toScript.Write(",");
                toScript.Write("scriptCharset:");
                DomOperation.ToScript(toScript, ScriptCharset);
            }
            if (StatusCode != null && StatusCode.Count > 0)
            {
                toScript.Write(",");
                toScript.Write("statusCode:");
                DomOperation.ToScript(toScript, StatusCode);
            }
            if (Success != null)
            {
                toScript.Write(",");
                toScript.Write("success:");
                DomOperation.ToScript(toScript, Success);

            }
            if (Timeout.HasValue)
            {
                toScript.Write(",");
                toScript.Write("timeout:");
                DomOperation.ToScript(toScript, Timeout);

            }
            if (Traditional.HasValue)
            {
                toScript.Write(",");
                toScript.Write("traditional:");
                DomOperation.ToScript(toScript, Traditional.Value);

            }
            if (!string.IsNullOrEmpty(Type))
            {
                toScript.Write(",");
                toScript.Write("type:");
                DomOperation.ToScript(toScript, Type);
            }
            if (!string.IsNullOrEmpty(UserName))
            {
                toScript.Write(",");
                toScript.Write("userName:");
                DomOperation.ToScript(toScript, UserName);
            }
            if (Xhr != null)
            {
                toScript.Write(",");
                toScript.Write("xhr:");
                DomOperation.ToScript(toScript, Xhr);
            }
            if (XhrFields != null)
            {
                toScript.Write(",");
                toScript.Write("xhrFields:");
                DomOperation.ToScript(toScript, XhrFields);
            }

            toScript.Write("}");
        }

        #endregion
    }
}
